<template>
    <div>
        <!--        面包屑导航区-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
<!--        按钮和表单区域-->
        <el-card>
            <el-col> <el-button>添加角色</el-button></el-col>
            <el-table :data="rolelist">
                <el-table-column type="expand">
<!--                    slot-scopr=scope接受数据-->
                    <template slot-scope="scope">
<!--                        栅格系统进行渲染 其中每一个el-row都默认分成了24列-->
                        <el-row v-for="(item1,i1) in scope.row.children" :key="item1.id" :class="['bdbottom',i1 === 0 ? 'bdtop' : '','vcenter']">
<!--                            一级权限划分-->
                            <el-col :span="5">
                                <el-tag closable @close="removeRightById(scope.row,item1.id)" >{{item1.authName}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
<!--                            二级\三级权限-->
                            <el-col :span="19">
<!--                                通过For循环 嵌套渲染二级权限。-->
                                <el-row :class="[i2 === 0 ? '' : 'bdtop','vcenter']" v-for="(item2,i2) in item1.children" :key="item2.id">
                                    <el-col :span="6">
                                        <el-tag closable @close="removeRightById(scope.row,item2.id)" type="success">{{item2.authName}}</el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <el-col :span="18">
                                        <el-tag closable @close="removeRightById(scope.row,item3.id)" v-for="(item3,i3) in item2.children" :key="item3.id" type="warning">{{item3.authName}}</el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>

                        </el-row>
                    </template>
                </el-table-column>
<!--                索引列-->
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="操作" width="500px">
                    <el-tooltip class="item" effect="dark" content="编辑" placement="top-start" :enterable="false">
                        <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                    </el-tooltip>
                    <el-tooltip  class="item" effect="dark" content="删除" placement="top-start" :enterable="false">
                        <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="分配权限" placement="top-start" :enterable="false">
                        <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetRightDialog">分配权限</el-button>
                    </el-tooltip>
                </el-table-column>
            </el-table>
        </el-card>
<!--        分配权限对话框-->
        <el-dialog
                title="分配权限"
                :visible.sync="setRightDialogVisible"
                width="50%">
<!--            树形控件-->
            <el-tree :data="rightslist" :props="treeProps"></el-tree>
            <span slot="footer" class="dialog-footer">
    <el-button @click="setRightDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="setRightDialogVisible = false">确 定</el-button>
  </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Roles",
        data(){
            return {
                //所有角色列表数据
                rolelist: [],
                setRightDialogVisible: false,
                //获取所有权限的列表
                rightslist:[],
                //树形控件的绑定对象
                treeProps:{
                    label:'authName',
                    children:'children'
                },

            }
        },
        created() {
          this.getRolesList()
        },
        methods: {
            //获取所有角色的列表
            async getRolesList() {
                const {data:res} = await this.$http.get('roles');
                if (res.meta.status !== 200) {
                    return this.$message.error('获取角色列表失败！');
                }
                this.rolelist = res.data;
                console.log(this.rolelist);
            },
            async removeRightById(role,rightId) {
                const confirmResult = await this.$confirm('是否要删除','提示',{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消',
                    type:'warning',
                }).catch(err => err)
                console.log(confirmResult);
                if (confirmResult !== 'confirm'){
                    return this.$message.info('取消了删除')
                }
                const {data:res} = await this.$http.delete(`roles/${role.id}/rights/${rightId}`);
                if (res.meta.status !== 200) {
                    return this.$message.error('删除权限失败！');
                }
                this.$message.success('删除权限成功！')
                role.children = res.data
            },
            async showSetRightDialog() {
                //获取权限数据
                const {data:res} = await this.$http.get('rights/tree')
                if (res.meta.status !== 200) {
                    return this.$message.error('获取权限数据失败！');
                }
                //把获取的数据保存到Data中
                this.rightslist = res.data;
                console.log(this.rightslist)


                //展示分配权限的对话框
                this.setRightDialogVisible = true;
            }

        }
    }
</script>

<style lang="less" scoped>
    .el-tag {
        margin: 7px;
    }
    .bdtop {
        border-top: 1px solid #eeeeee;
    }
    .bdbottom{
        border-bottom: 1px solid #eeeeee;
    }
    .vcenter {
        display: flex;
        align-items: center;
    }
</style>